﻿using Engine;
using Engine.Graphics;
using Game;

namespace Mekiasm
{
    public abstract class ClothingItem : Item
    {
        public ClothingBlock clothingBlock;

        protected ClothingData Clothing;

        public ClothingItem(string n) : base(n)
        {
            Clothing = new ClothingData();
            Clothing.MovementSpeedFactor = 1f;
            Clothing.CanBeDyed = false;
            Clothing.IsOuter = IsOuter();
            Clothing.Texture = GetTexture();
            Clothing.Slot = GetClothingSlot();
            Clothing.MovementSpeedFactor = 1f;

            Clothing.Mount = (value, clothing) => {
                ClothingEquiped(value, clothing.m_componentPlayer);

            };
            Clothing.Dismount = (value, clothing) => {
                ClothingUnEquiped(value, clothing.m_componentPlayer);

            };
            Clothing.Update = (value, clothing) => {
                ClothingEquipedUpdate(value, clothing.m_componentPlayer);
            };
        }
        public virtual ClothingSlot GetClothingSlot()
        {
            return ClothingSlot.Torso;
        }

        public virtual bool IsOuter()
        {
            return false;
        }

        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            if (clothingBlock == null) clothingBlock = BlocksManager.Blocks[ClothingBlock.Index] as ClothingBlock;
            Matrix matrix2 = ClothingBlock.m_slotTransforms[(int)GetClothingSlot()] * Matrix.CreateScale(size) * matrix;
            if (IsOuter())
            {
                BlocksManager.DrawMeshBlock(primitivesRenderer, clothingBlock.m_outerMesh, GetTexture(), color, 1f, ref matrix2, environmentData);
            }
            else
            {
                BlocksManager.DrawMeshBlock(primitivesRenderer, clothingBlock.m_innerMesh, GetTexture(), color, 1f, ref matrix2, environmentData);
            }
        }
        public override void ClothingEquiped(int value, ComponentPlayer componentPlayer)
        {
            DisplayMsg(componentPlayer, "装载");
        }
        public override void ClothingUnEquiped(int value, ComponentPlayer componentPlayer)
        {
            DisplayMsg(componentPlayer, "卸载");
        }
        public override bool OnUse(Ray3 aim, ComponentMiner componentMiner, TerrainRaycastResult? raycast)
        {
            return true;
        }

        public override ClothingData GetClothingData()
        {
            return Clothing;
        }
        public override string GetCategory(int value)
        {
            return MekiasmInit.categories_base_clothing;
        }

    }
}
